home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / h / AParser.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  6.7 KB  |  221 lines  |  [TEXT/MPS ]

  1. /* ANTLRParser.h
  2.  *
  3.  * Define the generic ANTLRParser superclass, which is subclassed to
  4.  * define an actual parser.
  5.  *
  6.  * Before entry into this file: TokenType must be set.
  7.  *
  8.  * SOFTWARE RIGHTS
  9.  *
  10.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  11.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  12.  * company may do whatever they wish with source code distributed with
  13.  * PCCTS or the code generated by PCCTS, including the incorporation of
  14.  * PCCTS, or its output, into commerical software.
  15.  * 
  16.  * We encourage users to develop software with PCCTS.  However, we do ask
  17.  * that credit is given to us for developing PCCTS.  By "credit",
  18.  * we mean that if you incorporate our source code into one of your
  19.  * programs (commercial product, research project, or otherwise) that you
  20.  * acknowledge this fact somewhere in the documentation, research report,
  21.  * etc...  If you like PCCTS and have developed a nice tool with the
  22.  * output, please mention that you developed it using PCCTS.  In
  23.  * addition, we ask that this header remain intact in our source code.
  24.  * As long as these guidelines are kept, we expect to continue enhancing
  25.  * this system and expect to make other tools available as they are
  26.  * completed.
  27.  *
  28.  * ANTLR 1.23
  29.  * Terence Parr
  30.  * Parr Research Corporation
  31.  * with Purdue University and AHPCRC, University of Minnesota
  32.  * 1989-1994
  33.  */
  34.  
  35. #ifndef APARSER_H_GATE
  36. #define APARSER_H_GATE
  37.  
  38. #include <stdio.h>
  39. #include <setjmp.h>
  40. #include "config.h"
  41. #include ATOKEN_H
  42. #include ATOKENBUFFER_H
  43.  
  44. #ifdef ZZCAN_GUESS
  45. #ifndef ZZINF_LOOK
  46. #define ZZINF_LOOK
  47. #endif
  48. #endif
  49.  
  50.  
  51. #define NLA            (token_type[lap&(LLk-1)])/* --> next LA */
  52.  
  53. typedef unsigned char SetWordType;
  54.  
  55. /* Define external bit set stuff (for SetWordType) */
  56. #define EXT_WORDSIZE    (sizeof(char)*8)
  57. #define EXT_LOGWORDSIZE    3
  58.  
  59.            /* s y n t a c t i c  p r e d i c a t e  s t u f f */
  60.  
  61. typedef struct _zzjmp_buf {
  62.             jmp_buf state;
  63.         } zzjmp_buf;
  64.  
  65. /* these need to be macros not member functions */
  66. #define zzGUESS_BLOCK        ANTLRParserState zzst; int zzrv; int _marker;
  67. #define zzNON_GUESS_MODE    if ( !guessing )
  68. #define zzGUESS_FAIL        guess_fail();
  69. #define zzGUESS_DONE        {inputTokens->rewind(_marker); guess_done(&zzst);}
  70. #define zzGUESS                saveState(&zzst); \
  71.                             guessing = 1; \
  72.                             _marker = inputTokens->mark(); \
  73.                             zzrv = setjmp(guess_start.state);
  74.  
  75.                   /* a n t l r  p a r s e r  d e f */
  76.  
  77. struct ANTLRParserState {
  78.     /* class variables */
  79.     zzjmp_buf guess_start;
  80.     int guessing;
  81.  
  82.     int inf_labase;
  83.     int inf_last;
  84.  
  85.     int dirty;
  86. };
  87.  
  88. /* notes:
  89.  *
  90.  * multiple inheritance is a cool way to include what stuff is needed
  91.  * in this structure (like guess stuff).  however, i'm not convinced that
  92.  * multiple inheritance works correctly on all platforms.  not that
  93.  * much space is used--just include all possibly useful members.
  94.  *
  95.  * the class should also be a template with arguments for the lookahead
  96.  * depth and so on.  that way, more than one parser can be defined (as
  97.  * each will probably have different lookahead requirements).  however,
  98.  * am i sure that templates work?  no, i'm not sure.
  99.  *
  100.  * no attributes are maintained and, hence, the 'asp' variable is not
  101.  * needed.  $i can still be referenced, but it refers to the token
  102.  * associated with that rule element.  question: where are the token's
  103.  * stored if not on the software stack?  in local variables created
  104.  * and assigned to by antlr.
  105.  */
  106. class ANTLRParser {
  107. protected:
  108.     /* class variables */
  109.     static SetWordType bitmask[sizeof(SetWordType)*8];
  110.     static char eMsgBuffer[500];
  111.  
  112. protected:
  113.     int LLk;                    // number of lookahead symbols (old LL_K)
  114.     int demand_look;
  115.     TokenType eofToken;            // when do I stop during resynch()s
  116.     int bsetsize;                // size of bitsets created by ANTLR in
  117.                                 // units of SetWordType
  118.  
  119.     ANTLRTokenBuffer *inputTokens;    //place to get input tokens
  120.  
  121.     zzjmp_buf guess_start;        // where to jump back to upon failure
  122.     int guessing;                // if guessing (using (...)? predicate)
  123.  
  124.     // infinite lookahead stuff
  125.     int can_use_inf_look;        // set by subclass (generated by ANTLR)
  126.     int inf_lap;
  127.     int inf_labase;
  128.     int inf_last;
  129.     ANTLRTokenBase **inf_token;
  130.     int *_inf_line;
  131.  
  132.     ANTLRChar **token_tbl;        // pointer to table of token type strings
  133.  
  134.     int dirty;                    // used during demand lookahead
  135.  
  136.     TokenType *token_type;        // fast reference cache of token.getType()
  137. //    ANTLRLightweightToken **token;    // the token with all its attributes
  138.     int lap;
  139.     int labase;
  140.  
  141. private:
  142.     void fill_inf_look();
  143.  
  144. protected:
  145.     void guess_fail()                { longjmp(guess_start.state, 1); }
  146.     void guess_done(ANTLRParserState *st){ restoreState(st); }
  147.     int guess(ANTLRParserState *);
  148.     void look(int);
  149.     int _match(TokenType, ANTLRChar **, TokenType *,
  150.                ANTLRLightweightToken **, SetWordType **);
  151.     int _setmatch(SetWordType *, ANTLRChar **, TokenType *,
  152.                ANTLRLightweightToken **, SetWordType **);
  153.     virtual void consume();
  154.     void resynch(SetWordType *wd,SetWordType mask);
  155.     void prime_lookahead();
  156.     virtual void tracein(char *r)
  157.             {
  158.                 fprintf(stderr, "enter rule \"%s\"\n", r);
  159.             }
  160.     virtual void traceout(char *r)
  161.             {
  162.                 fprintf(stderr, "exit rule \"%s\"\n", r);
  163.             }
  164.     unsigned MODWORD(unsigned x) {return x & (EXT_WORDSIZE-1);}    // x % EXT_WORDSIZE
  165.     unsigned DIVWORD(unsigned x) {return x >> EXT_LOGWORDSIZE;}    // x / EXT_WORDSIZE
  166.     int set_deg(SetWordType *);
  167.     int set_el(TokenType, SetWordType *);
  168.     void edecode(SetWordType *);
  169.     void FAIL(int k, ...);
  170.  
  171. public:
  172.     ANTLRParser(ANTLRTokenBuffer *,
  173.                 int k=1,
  174.                 int use_inf_look=0,
  175.                 int demand_look=0,
  176.                 int bsetsize=1);
  177.     virtual ~ANTLRParser();
  178.  
  179.     virtual void init();
  180.     
  181.     TokenType LA(int i)
  182.     {
  183.         return demand_look ? token_type[(labase+(i)-1)&(LLk-1)] :
  184.                             token_type[(lap+(i)-1)&(LLk-1)];
  185.     }
  186.     ANTLRTokenBase *LT(int i);
  187.  
  188.     void setEofToken(TokenType t) { eofToken = t; }
  189.  
  190.     void syn(ANTLRTokenBase *tok, ANTLRChar *egroup,
  191.              SetWordType *eset, TokenType etok, int k);
  192.     void saveState(ANTLRParserState *);
  193.     void restoreState(ANTLRParserState *);
  194.  
  195.     void panic(char *msg);
  196.     static char *eMsgd(char *,int);
  197.     static char *eMsg(char *,char *);
  198.     static char *eMsg2(char *,char *,char *);
  199. };
  200.  
  201.  
  202. #define zzmatch(_t)                            \
  203.     if ( !_match((TokenType)_t, &zzMissText, &zzMissTok, \
  204.                  (ANTLRLightweightToken **) &zzBadTok, &zzMissSet) ) goto fail;
  205.  
  206. #define setmatch(_ts)                            \
  207.     if ( !_setmatch(_ts, &zzMissText, &zzMissTok, \
  208.                  (ANTLRLightweightToken **) &zzBadTok, &zzMissSet) ) goto fail;
  209.  
  210. #ifndef zzfailed_pred
  211. #define zzfailed_pred(_p)       \
  212.         zzNON_GUESS_MODE { fprintf(stderr, "semantic error; failed predicate: '%s'\n",_p); }
  213. #endif
  214.  
  215. #define zzRULE        SetWordType *zzMissSet=NULL; TokenType zzMissTok=(TokenType)0;    \
  216.                     ANTLRTokenBase *zzBadTok; ANTLRChar *zzBadText=(ANTLRChar *)"";    \
  217.                     int zzErrk=1;                                    \
  218.                     ANTLRChar *zzMissText=(ANTLRChar *)"";
  219.  
  220. #endif
  221.